<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>期刊上架检索系统</h2>
        <p>请登录以使用编辑和删除功能</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            size="large"
            clearable
          >
            <template #prefix>
              <span style="font-size: 16px;">👤</span>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            size="large"
            show-password
            clearable
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <span style="font-size: 16px;">🔒</span>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="code">
          <div class="captcha-row">
            <el-input
              v-model="loginForm.code"
              placeholder="验证码"
              size="large"
              clearable
              @keyup.enter="handleLogin"
            >
              <template #prefix>
                <span style="font-size: 16px;">🖼️</span>
              </template>
            </el-input>
            <div class="captcha-image" @click="refreshCaptcha">
              <img v-if="userStore.captchaImg" :src="userStore.captchaImg" alt="验证码" />
              <div v-else class="captcha-placeholder">点击获取验证码</div>
            </div>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="loading"
            class="login-button"
            @click="handleLogin"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>

      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

const loginFormRef = ref(null)
const loading = ref(false)
const errorMessage = ref('')

const loginForm = reactive({
  username: '',
  password: '',
  code: '',
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
  ],
}

// 获取验证码
const refreshCaptcha = async () => {
  const result = await userStore.fetchCaptcha()
  if (!result.success) {
    ElMessage.error(result.message || '获取验证码失败')
  }
}

// 登录
const handleLogin = async () => {
  if (!loginFormRef.value) return

  await loginFormRef.value.validate(async (valid) => {
    if (!valid) return

    loading.value = true
    errorMessage.value = ''

    try {
      const result = await userStore.userLogin(loginForm)
      
      if (result.success) {
        ElMessage.success('登录成功')
        // 跳转到首页
        router.push('/')
      } else {
        errorMessage.value = result.message || '登录失败'
        // 登录失败后刷新验证码
        await refreshCaptcha()
        loginForm.code = ''
      }
    } catch (error) {
      errorMessage.value = error.message || '登录失败'
      await refreshCaptcha()
      loginForm.code = ''
    } finally {
      loading.value = false
    }
  })
}

// 初始化时获取验证码
onMounted(() => {
  refreshCaptcha()
})
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
}

.login-header h2 {
  font-size: 24px;
  font-weight: 600;
  color: #1f2a44;
  margin: 0 0 8px 0;
}

.login-header p {
  font-size: 14px;
  color: #60708f;
  margin: 0;
}

.login-form {
  margin-top: 32px;
}

.captcha-row {
  display: flex;
  gap: 12px;
  width: 100%;
}

.captcha-row .el-input {
  flex: 1;
}

.captcha-image {
  width: 120px;
  height: 40px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  cursor: pointer;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f7fa;
  transition: all 0.2s;
}

.captcha-image:hover {
  border-color: #409eff;
  background: #f0f4ff;
}

.captcha-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.captcha-placeholder {
  font-size: 12px;
  color: #909399;
  text-align: center;
  padding: 0 8px;
}

.login-button {
  width: 100%;
  margin-top: 8px;
}

.error-message {
  margin-top: 16px;
  padding: 12px;
  background: #fef0f0;
  border: 1px solid #fde2e2;
  border-radius: 4px;
  color: #f56c6c;
  font-size: 14px;
  text-align: center;
}
</style>

